package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Tag", "/tag", "/TagServlet", "/tagServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class TagServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public TagServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml; charset=UTF-8");

		String mode = request.getParameter("mode");
		
		if (mode.equalsIgnoreCase("add")) {
			String error="0";
			String formattedTag="";
			String newTagList="";
			String id = request.getParameter("id");
			System.out.print("id: "+id);
			String table = request.getParameter("table");
			System.out.print("  table: "+table);	
			String field = request.getParameter("field");
			System.out.print("   field: "+field);	 
			String tag = request.getParameter("tag");
			System.out.print("   tag: "+tag);
			String oldTagList = request.getParameter("oldTagList");
			if(oldTagList=="")
				oldTagList="null";
			System.out.print("  oldTagList: "+oldTagList);
			formattedTag=tag.toLowerCase();
			if(tag.contains(","))
			{
				error="3";
				newTagList=oldTagList;
			}
			else
			{
				
				if(oldTagList=="null")
					newTagList=formattedTag;
				else
					newTagList=oldTagList+";"+formattedTag;
					
			}
			System.out.print("   error: "+error);
			System.out.print("   formattedTag: "+formattedTag);
			System.out.println("   newTagList: "+newTagList);
			PrintWriter out = response.getWriter();
			String resultXML = CommonUtil.createAjaxResultTagXML(error, formattedTag,oldTagList,newTagList);
			out.println(resultXML);
		}
		
		if (mode.equalsIgnoreCase("remove")) {
			String error="0";
			String formattedTag="";
			String newTagList="";
			String id = request.getParameter("id");
			System.out.print("id: "+id);
			String table = request.getParameter("table");
			System.out.print("  table: "+table);	
			String field = request.getParameter("field");
			System.out.print("   field: "+field);	 
			String tag = request.getParameter("tag");
			System.out.print("   tag: "+tag);
			formattedTag=tag;
			String oldTagList = request.getParameter("oldTagList");
			if(oldTagList=="")
				oldTagList="null";
			System.out.print("  oldTagList: "+oldTagList);
			String[] oldTagListComp=oldTagList.split(";");
			for(int i=0;i<oldTagListComp.length;i++)
			{
				if(!oldTagListComp[i].equals(tag))
				{
				//	System.out.print("  ko trung ");
					if(i==0)
						newTagList=newTagList+oldTagListComp[i];
					else
						newTagList=newTagList+";"+oldTagListComp[i];
				}
			//	else
			//		System.out.print("   trung ");
			}
			if(newTagList=="")
				newTagList="null";
					
			
			System.out.print("   error: "+error);
			
			System.out.println("   newTagList: "+newTagList);
			PrintWriter out = response.getWriter();
			String resultXML = CommonUtil.createAjaxResultTagXML(error, formattedTag,oldTagList,newTagList);
			out.println(resultXML);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		// TODO read from URL param
		
		
		
	}
}